import json
import pymongo

def response(flow):
    # 获取7X24 实时新闻
    if '/news/twenty-four-hour-news/add-news' in flow.request.url:
        res = flow.response
        data = json.loads(res.text)['result']['data']['data']
        if data :
            col = get_mongo_col('xinlang_app','new')
            for item in data:
                if col.find({'id':item['id']}):
                    col.delete_many({'id':item['id']})
            col.insert(data)

def get_mongo_col(db,col):
    client=pymongo.MongoClient('mongodb://192.168.0.21:27017')
    mydb = client[db]
    mycol = mydb[col]
    return mycol

